package seace.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seace.services.Service;
import seace.utils.Constants;

@SuppressWarnings("serial")
public class ControllerServlet extends HttpServlet {

	@SuppressWarnings("unchecked")
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String business = request.getParameter(Constants.EVENTO_NOME);
		String className = Constants.EVENTO_NOME + ".services." + business;
		try {

			Class classe = Class.forName(className);
			if (!Service.class.isAssignableFrom(classe)) {
				throw new ServletException("Classe não implementa interface"
						+ className);
			}
			Service service = (Service) classe.newInstance();
			service.execute(request, response);
			
		} catch (Exception e) {
			throw new ServletException("A lógica causou uma excessão", e);
		}
	}
}
